package chixing.day05;

import java.util.Arrays;

/**
 * User（用户类）有属性:userId（用户编号）、userName（姓名），Book[] borrowedBooks（借阅书籍列表，Book[] 类型,最多可借阅3本书）
 */
public class User {
    private int userId;
    private String userName;
    public Book[] borrowedBooks = new Book[3];
    public User(){}
    public User(int userId, String userName) {
        this.userId = userId;
        this.userName = userName;
    }

    public User(int userId, String userName, Book[] borrowedBooks) {
        this(userId,userName);
        this.borrowedBooks = borrowedBooks;
    }


    //查看用户借阅列表已经存在几本书
    public int bnum(){
        int count=0;
        //查找borrowBook中有几本书，以便于确定插入书籍时插入的当前下标，并且查看borrowebook中是否已经放满三本书
        for (int i = 0;i < borrowedBooks.length;i++){
            if (borrowedBooks[i] != null){
                count++;
            }
        }
        return count;
    }
    /**
     * 提供借书 borrowBook(Book book) 方法：
     * 如果书可借，设置为可借，加入 borrowedBooks 列表。
     * 否则提示该书已被借出。
     * @param book 要判断的图书的对象
     * @return 若可借返回图书的属性，否则提示该书已被借出
     */
    public void borrowBook(Book book){
        int count = bnum();
        System.out.println("-------------------------");
        //是否可借若可借加入borrowBook，否则提示该书被借出
        if (count == 3 ){
            System.out.println("你的借阅已达上限！");
        } else if (book.isAvailable()) {
            //应该把图书馆中的是否可借改为false，而不是只把用户借阅列表中的可借改为false
            book.setAvailable(false);
            borrowedBooks[count] = book;
            System.out.println("借书成功");
        }else {
            System.out.println(book.getBookTitle() + "书已被借出");
        }
    }


    /**
     * 提供还书 returnBook(String isbn) 方法：
     * 还完书之后要把用户借书列表中的书籍改为null，并且要把用户借书列表中的书往前移
     * @param isbn 要还书籍的编号
     */
    public void returnBook(String isbn){
        System.out.println("-------------------------");
        int count = bnum();
        for (int i = 0;i < count;i++){
            if (isbn.equals(borrowedBooks[i].getBookIsbn())){
                //将对应图书从借阅列表中移除，并设置为可借。
                //应该把图书馆列表中的false改为true
                borrowedBooks[i].setAvailable(true);
                //直接把最后一本书的信息覆盖掉要换属的信息然后再把最后一本书改为null
                borrowedBooks[i] = borrowedBooks[count-1];
                //把最后一本书改为null
                borrowedBooks[count-1] = null;
                System.out.println(isbn + ",还书成功!");
                break;
            }
        }
    }
    //提供 listBorrowedBooks() 方法，打印当前借阅的所有书籍。
    public void listBorrowedBooks(){
        System.out.println("-------------------------");
        System.out.println("用户" + userName + "借阅的书籍有:");
        for (Book borrowedBook : borrowedBooks) {
            if (borrowedBook != null) {
                System.out.println(borrowedBook);
            }
        }
        //打印用户列表所有内容
//        for (int i = 0;i <borrowedBooks.length;i++){
//            System.out.println(borrowedBooks[i]);
//        }
    }


    public int getUserId() {
        return userId;
    }

    public void setUserId(int userId) {
        this.userId = userId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public Book[] getBorrowedBooks() {
        return borrowedBooks;
    }

    public void setBorrowedBooks(Book[] borrowedBooks) {
        this.borrowedBooks = borrowedBooks;
    }

    @Override
    public String toString() {
        return "User{" +
                "userId=" + userId +
                ", userName='" + userName + '\'' +
                ", borrowedBooks=" + Arrays.toString(borrowedBooks) +
                '}';
    }
}
